<?php

/**
 * This is the model class for table "tbl_invsubcategories".
 *
 * The followings are the available columns in table 'tbl_invsubcategories':
 * @property integer $id
 * @property integer $category
 * @property string $subcategory
 * @property string $applymarkup
 * @property string $markupcode
 * @property string $pricebook
 * @property integer $status
 * @property string $image
 * 
 */
class Invsubcategories extends CActiveRecord
{
    public $file;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Invsubcategories the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_invsubcategories';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('category, subcategory', 'required'),
			array('category, status', 'numerical', 'integerOnly'=>true),
			array('subcategory', 'length', 'max'=>75),
			array('applymarkup', 'length', 'max'=>12),
			array('markupcode', 'length', 'max'=>2),
			array('pricebook', 'length', 'max'=>4),
                        array('file','file','types'=>'jpg,png,jpeg,bmp,gif','maxSize'=>1000000,'allowEmpty'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, category, subcategory, applymarkup, markupcode, pricebook, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'category' => 'Category',
			'subcategory' => 'Sub Category',
			'applymarkup' => 'Apply Markup',
			'markupcode' => 'Markup Code',
			'pricebook' => 'Price Book',
			'status' => 'Status',
                        'image' => 'Image'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('category',$this->category);
		$criteria->compare('subcategory',$this->subcategory,true);
		$criteria->compare('applymarkup',$this->applymarkup,true);
		$criteria->compare('markupcode',$this->markupcode,true);
		$criteria->compare('pricebook',$this->pricebook,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        public function beforeSave(){
            
            
            $this->file=CUploadedFile::getInstance($this, 'file');
            if($this->file!=NULL){
                    //made a random name base on timestamp
                $ext=getFileExt($this->file);
                $name=getFileBodyName($this->file);


                $this->image=$name.'_'.time().$ext;
               
                $this->file->saveAs('images/content/subcategories/'.$this->image,true);   
            }
            return parent::beforeSave();
        }
        
        public function getStatusList(){
            return array(
                0=>'Active',
                -1=>'Inactive',
                
            );
        }
        public function getStatusText(){
            return isset($this->statusList[$this->status])?$this->statusList[$this->status]:NULL;
        }
        
        public function getCategoryList(){
            $data=Yii::app()->db->createCommand('select * from tbl_invcategories where status=0')->queryAll();
            $rs=array();
            if(count($data)>0){
                foreach($data as $item){
                    $rs[$item['id']]=$item['categories'];
                }
            }
            return $rs;
        }
        
        public function getCategoryText(){
            if($this->category!=NULL){
                $data=Yii::app()->db->createCommand('select categories from tbl_invcategories where id=:id')->bindValue('id', $this->category)->queryScalar();
                return $data;
            }
            return NULL;
        }
        public function getApplyMakupList(){
            return array(
                'Average Cost'=>'Average Cost',
                'Base Cost'=>'Base Cost'
            );
        }
        
}